@extends('admin.home.main')

@section('body')
<section id="main-content">

<section class="wrapper">
<h3>{{$courseinfo->name}}->视频列表</h3>
<hr>
<!-- row -->
<div class="row mt">
  <div class="col-md-12">
    <div class="content-panel">
      <table class="table table-striped table-advance table-hover">
        <thead>
          <tr>
          	<th>#</th>
            <th>视频名称</th>
            <th>添加时间</th>
            <th>是否免费</th>
            <th>是否可分享</th>
            <th>
              操作 |
              <a href="javascript:;" onclick="setorder()">设置排序</a> |
              <a href="javascript:;" id="saveorder" onclick="saveorder()" style="display:none;">保存排序</a>
              <input type="hidden" id="_token" name="_token" value="{{csrf_token()}}">
            </th>
          </tr>
        </thead>
        <tbody>
          @foreach($videolist as $vtm=>$vst)
            <tr>
              <td>{{$vtm+1}}</td>
              <td class="title" video_id="{{$vst->id}}"><a href="{{$vst->video_link}}">第{{$vst->video_name}}集</a></td>
              <td>{{$vst->add_date}}</td>
              <td>@if($vst->is_free == 1) 不免费 @else 免费 @endif</td>
              <td>@if($vst->is_share == 1) 可分享 @else 不可分享 @endif</td>  
              <td>
                <a href="/admin/course/video_edit/{{$vst->id}}/num/{{$vtm+1}}" class="btn btn-primary btn-xs">编辑</a>
                <button type="button" colid="{{$vst->id}}" class="btn btn-danger btn-xs col_del">删除</button>
              </td>
            </tr>
          @endforeach
        </tbody>
      </table>
    </div>
    <!-- /content-panel -->
  </div>
  <!-- /col-md-12 -->
</div>
<!-- /row -->
</section>
</section>
<script type="text/javascript">
  $('.col_del').click(function(){
    var _colId = $(this).attr('colid');

    var a = confirm('请确认是否要删除该视频');
    var t = $(this).parent().parent();

    if(a == true){
      $.ajax({
        'type':'get',
        'url':'/admin/course/video_del/'+_colId,
        'dataType':'json',
        success:function(msg){
          if(msg.code == 200){
            alert('删除成功');
          }else{
            alert('删除失败');
          }
          location.reload();
        }
      })
    }
  })

  function setorder(){
    var title = $('.title')
    title.html('<input type="" class="set-order-by" value="">')
    $('#saveorder').removeAttr('style');
  }

  function saveorder(){
    var courseID = {{$courseinfo->id}};
    var _token = $('#_token').val();
    var orderby = [];
    var videoid = [];
    $('.set-order-by').each(function(){
      if($(this).val() != ''){
        orderby.push($(this).val());
        videoid.push($(this).parent().attr('video_id'));
      }
    })

    $.ajax({
      url:'/admin/course/set_order_by',
      type:'post',
      data:{'_token':_token,'courseid':courseID,'videoid':videoid,'orderby':orderby},
      success:function(res){
        if(res.code == 200){
          alert('设置排序成功');
        }else{
          alert('设置排序失败');
        }
        location.reload();
      }
    })
  }
</script>
@endsection